package com.tms.app.tms.app;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.alibaba.sdk.android.push.register.HuaWeiRegister;
import com.alibaba.sdk.android.push.register.MiPushRegister;
import com.tms.app.tms.utils.SharePreferenceUtil;

import org.xutils.DbManager;
import org.xutils.x;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by hl on 2017/5/24.
 */

public class MyApp extends Application {
   private static MyApp myapp;
    private static  String TAG="app";
    public static String DeviceId=null;
    public static Map<String,Integer> loadingMap=new HashMap<>();
    public static MyApp getInstance() {
        return myapp;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        myapp=this;
        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);
        x.Ext.init(this);
        x.Ext.setDebug(true); // 是否输出debug日志, 开启debug会影响性能.
        initCloudChannel(this);
    }


    /**
     * 初始化云推送通道
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Log.d(TAG, "ali阿里云推送启动 success");
            }
            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.d(TAG, "ali阿里云推送启动 failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
        DeviceId=pushService.getDeviceId();
        System.out.print("设备id#######:"+pushService.getDeviceId());
        SharePreferenceUtil.setValue(this,"deviceId",pushService.getDeviceId());
        MiPushRegister.register(applicationContext, "2882303761517680742", " 5461768073742");
// 注册方法会自动判断是否支持华为系统推送，如不支持会跳过注册。
        HuaWeiRegister.register(applicationContext);

    }


    public static DbManager.DaoConfig getDaoConfig(){

            return  new DbManager.DaoConfig()
                .setDbName("test.db")
                // 不设置dbDir时, 默认存储在app的私有目录.
                // .setDbDir(new File("/sdcard/tms/bdorc/img/db/")) // "sdcard"的写法并非最佳实践, 这里为了简单, 先这样写了.
                .setDbVersion(11)
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        // 开启WAL, 对写入加速提升巨大
                        db.getDatabase().enableWriteAheadLogging();
                    }
                })
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                        // TODO: ...
                        // db.addColumn(...);
                        // db.dropTable(...);
                        // ...
                        // or
                        // db.dropDb();
                    }
                });

    }

}
